package com.evil.application.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.evil.application.pojo.dto.order.node.handler.QueryOrderNodeHandler;
import com.evil.application.pojo.entity.ApplicationOrderNodeHandler;
import com.evil.application.util.ApplicationUtil;
import com.evil.common.application.dto.order.node.OrderNodesDTO;
import org.apache.ibatis.annotations.Param;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 应用订单节点处理人表
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
public interface ApplicationOrderNodeHandlerMapper extends BaseMapper<ApplicationOrderNodeHandler> {

    boolean hadOrderNodeHandler(QueryOrderNodeHandler queryOrderNodeHandler);

    List<ApplicationOrderNodeHandler> queryOrderNodeHandler(QueryOrderNodeHandler queryOrderNodeHandler);

    List<ApplicationOrderNodeHandler> orderNodeHandlers(@Param("orderId") Long orderId, @Param("nodeId") Long nodeId);

    List<ApplicationOrderNodeHandler> orderNodesHandlers(@Param("orderNodes") OrderNodesDTO orderNodes);

    default Map<String, List<ApplicationOrderNodeHandler>> orderNodesHandlersMap(@Param("orderNodes") OrderNodesDTO orderNodes) {
        return this.orderNodesHandlers(orderNodes).stream()
                .collect(Collectors.groupingBy(e -> ApplicationUtil.orderNodeKey(e.getOrderId(), e.getNodeId())));
    }

    List<ApplicationOrderNodeHandler> ordersNodesHandlers(@Param("orderNodes") List<OrderNodesDTO> orderNodes);

    List<ApplicationOrderNodeHandler> findProcessingOrderNode(@Param("orderId") Long orderId, @Param("handleType") Integer handleType);

    List<ApplicationOrderNodeHandler> findByOrderIds(@Param("orderIds") Collection<Long> orderIds);

    default Map<Long, List<ApplicationOrderNodeHandler>> findMapByOrderIds(@Param("orderIds") Collection<Long> orderIds) {
        return this.findByOrderIds(orderIds).stream().collect(Collectors.groupingBy(ApplicationOrderNodeHandler::getOrderId));
    }
}